package com.skirmisher.inspection.conform.service.impl;

import com.skirmisher.inspection.common.entity.InsFile;
import com.skirmisher.inspection.common.mapper.InsFileMapper;
import com.skirmisher.inspection.common.service.SystemCodeCacheService;
import com.skirmisher.inspection.common.sysenum.InsJobStatusEnum;
import com.skirmisher.inspection.conform.entity.*;
import com.skirmisher.inspection.conform.mapper.InsConformJobItemsMapper;
import com.skirmisher.inspection.conform.mapper.InsConformJobMapper;
import com.skirmisher.inspection.conform.mapper.InsConformJobWorkerMapper;
import com.skirmisher.inspection.conform.service.InsConformJobService;
import com.skirmisher.inspection.daily.entity.InsDailyJob;
import com.skirmisher.inspection.inspection.testController.ResponseModel;
import com.skirmisher.inspection.rectify.entity.InsJobError;
import com.skirmisher.inspection.rectify.mapper.InsJobErrotMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName InsConformJobServiceImpl
 * @Description TODO
 * @Author hanwangxin
 * @Date 2020/12/17 下午7:48
 * @Version 1.0
 */
@Service
@Transactional
@Slf4j
public class InsConformJobServiceImpl implements InsConformJobService {

    @Autowired
    private InsConformJobMapper insConformJobMapper;
    @Autowired
    private InsConformJobItemsMapper insConformJobItemsMapper;
    @Autowired
    private InsConformJobWorkerMapper insConformJobWorkerMapper;
    @Autowired
    private InsJobErrotMapper insJobErrotMapper;

    @Autowired
    private SystemCodeCacheService systemCodeCacheService;
    @Autowired
    private InsFileMapper insFileMapper;

    @Override
    public InsConformJob getJobDetail(String jobId) {
        InsConformJob insConformJob = insConformJobMapper.getById(jobId);
        if (insConformJob == null){
            return null;
        }
        List<InsConformJobItems> items = insConformJobItemsMapper.getByJobId(jobId);
        insConformJob.setItems(items);
        List<InsConformJobWorker> workers = insConformJobWorkerMapper.getByBindId(jobId);
        insConformJob.setWorkers(workers);
        List<InsJobError> insJobErrors = insJobErrotMapper.errorList(jobId);
        for (InsJobError insJobError : insJobErrors) {
            String id = insJobError.getId();
            List<InsFile> fileList = insFileMapper.getFileList(id);
            insJobError.setFiles(fileList);
        }
        insConformJob.setErrors(insJobErrors);
        return insConformJob;
    }

    @Override
    public void getJobPageData(InsConformJobQueryEntity query) {

        List<Object> dataList = insConformJobMapper.pageQueryData(query);
        int count = insConformJobMapper.pageQueryCount(query);

        query.setDateList(dataList);
        query.setCount(count);
    }

    @Override
    public void deletConformJob(List<String> jobIds) {

        for (String jobId : jobIds) {
            InsConformJob job = new InsConformJob();
            job.setId(jobId);
            job.setJobStatus("99");
            insConformJobMapper.updateIgnoreNull(job);
        }
    }

    @Override
    public void saveConformJob(InsConformJobSave jobSave, ResponseModel responseModel) {

        //处理主单据数据
        InsConformJob conformJob = new InsConformJob();
        String jobId = UUID.randomUUID().toString();
        if(!StringUtils.isEmpty(jobSave.getId())){
            jobId = jobSave.getId();
        }
        BeanUtils.copyProperties(jobSave, conformJob);
        //填写其他参数
        conformJob.setId(jobId);
        //获取模板编码值
        String nextCode = systemCodeCacheService.getNextCode(SystemCodeCacheService.CONFORMJOB);
        conformJob.setJobCode(nextCode);
        Date now = new Date();
        conformJob.setRecTime(now);
        conformJob.setJobStatus(InsJobStatusEnum.READY2WORK.getStatus()+"");
        conformJob.setRecCode(jobSave.getWorkCode());
        conformJob.setRecName(jobSave.getWorkName());

        insConformJobMapper.insert(conformJob);
        //处理巡查项目数据
        List<InsConformJobItems> items = jobSave.getItems();
        if (items!=null){
            for (InsConformJobItems item : items) {
                item.setItemId(UUID.randomUUID().toString());
                item.setTempId(jobId);
                if (StringUtils.isEmpty(item.getItemCode())){
                    String noDateCode = systemCodeCacheService.getNoDateCode(SystemCodeCacheService.ITEMCODE);
                    item.setItemCode(noDateCode);
                }

                insConformJobItemsMapper.insert(item);
            }
        }


        //处理人员数据
        List<InsConformJobWorker> workers = jobSave.getWorkers();
        for (InsConformJobWorker worker : workers) {
            worker.setId(UUID.randomUUID().toString());
            worker.setBindId(jobId);
            worker.setJobId(jobId);
            insConformJobWorkerMapper.insert(worker);
        }
    }

    @Override
    public void updateConformJob(InsConformJobSave jobSave, ResponseModel responseModel) {

        //删除日常巡查模任务 内容 人员 周期
        String jobId = jobSave.getId();
        insConformJobMapper.deleteJob(jobId);
        insConformJobItemsMapper.deleteJobItem(jobId);
        insConformJobWorkerMapper.deleteJobWorker(jobId);

        saveConformJob(jobSave,responseModel);
    }

    @Override
    public void startJob(String jobId) {

        InsConformJob job = new InsConformJob();
        job.setId(jobId);
        job.setJobStatus(InsJobStatusEnum.WORKING.getStatus()+"");
        insConformJobMapper.updateIgnoreNull(job);
    }

    @Override
    public void endJob(String jobId) {

        //结束任务判断下该任务是否存在整改
        InsConformJob conformJob = insConformJobMapper.getById(jobId);
        String jobStatus = conformJob.getJobStatus();
        if (!(jobStatus != null && jobStatus.equals(InsJobStatusEnum.RECTIFYING.getStatus()+""))){
            InsConformJob job = new InsConformJob();
            job.setId(jobId);
            job.setJobStatus(InsJobStatusEnum.COMPLIE.getStatus()+"");
            insConformJobMapper.updateIgnoreNull(job);
        }
    }

    @Override
    public void updateConformJob2Send(InsConformJobSave jobSave, ResponseModel responseModel) {

        //删除日常巡查模任务 内容 人员 周期
        String jobId = jobSave.getId();
        insConformJobWorkerMapper.deleteJobWorker(jobId);
        //增加人员
        //处理人员数据
        List<InsConformJobWorker> workers = jobSave.getWorkers();
        for (InsConformJobWorker worker : workers) {
            worker.setId(UUID.randomUUID().toString());
            worker.setBindId(jobId);
            worker.setJobId(jobId);
            insConformJobWorkerMapper.insert(worker);
        }
        //更新job的状态
        InsConformJob job = new InsConformJob();
        job.setId(jobId);
        job.setJobStatus(InsJobStatusEnum.READY2SEND.getStatus()+"");
        insConformJobMapper.updateIgnoreNull(job);
    }
}
